package com.hundsun.cprs.yyt.service.form.field.wrap;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hundsun.cprs.yyt.service.form.dto.FormAttrInfo;
import com.hundsun.cprs.yyt.service.form.dto.FormInfo;
import com.hundsun.cprs.yyt.util.JDBCUtil;

public class SelectField extends AbstractField {
    
    private static final String STRING_SPLIT = ",";
    
    private static final String STRING_SPLIT_KEYVALUE = ":";
    
    /**
     * 下拉框选项值
     */
    private Map<String,String> options;
    
    /**
     * 该属性被引用的同行属性attrKey
     */
    private String nextAttrKey;
    
    public SelectField(FormAttrInfo formAttrInfo,FormInfo form) {
        super(formAttrInfo,form);
    }
    
    public Map<String, String> getOptions() {
        return options;
    }
    
    public String getOptionName(String key) {
        if (options != null) {
            return options.get(key);
        }
        return null;
    }

    public String getNextAttrKey() {
        return nextAttrKey;
    }

    @Override
    protected void render() {
        //初始化下拉框选项
        initSelectOption();
        
        //检查及加载同行级联引用属性
        checkAndLoadRefAttrKey();
    }
    
    private void initSelectOption(){
        Integer valueSouce = getValueSource() == null?0:getValueSource();
        switch (valueSouce) {
            case 1:
                {
                  //text格式，以key:value格式存储,多个逗号分隔
                  String[] result = StringUtils.split(getValueContent(), STRING_SPLIT);
                  options = new HashMap<String,String>();
                  if (result != null && result.length > 0) {
                      for(String temp : result){
                          String[] keyValue = StringUtils.split(temp, STRING_SPLIT_KEYVALUE);
                          options.put(keyValue[0], keyValue[1]);
                      }
                  }
                }
                break;
            case 2:
                {
                  //sql格式
                    List<Map<String,Object>> result = JDBCUtil.query(getValueContent());
                    if (result != null && result.size() > 0) {
                        options = new LinkedHashMap<String,String>();
                        for(Map<String,Object> item : result){
                            options.put(item.get("key").toString(), item.get("name").toString());
                        }
                    }
                }
                break;
            case 3:
                {
                    //json格式
                    JSONArray result =JSON.parseArray(getValueContent());
                    if (result != null && result.size() > 0) {
                        options = new HashMap<String,String>();
                        for(int i=0;i<result.size();i++){
                            JSONObject item = result.getJSONObject(i);
                            options.put(item.getString("key"), item.getString("name"));
                          }
                    }
                }
                break;
            default:
                break;
        }
    }
    
    
    private void checkAndLoadRefAttrKey(){
        if (getForm() == null) {
            return;
        }
        List<FormAttrInfo> otherFormAttrInfo = getForm().getFormAttrInfoList();
        for(FormAttrInfo item : otherFormAttrInfo){
            if (item.getRefAttrKey() != null && item.getRefAttrKey().equals(getAttrKey())) {
                nextAttrKey = item.getAttrKey();
            }
        }
    }
}
